/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewAgeClassificationEditor.java
 *
 * Created on 11.06.2009, 20:56:46
 */

package trackreggui;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import webreg.model.Sex;
import webreg.model.exceptions.EmptyDescriptionException;
import webreg.model.exceptions.NegativNumberException;
import webreg.model.swing.SexComboBoxModel;
import org.jdesktop.application.Action;
import webreg.model.AgeClassification;

/**
 *
 * @author STKNOLL
 */
public class ViewAgeClassificationEditor extends javax.swing.JDialog {

    /** Creates new form ViewAgeClassificationEditor */
    public ViewAgeClassificationEditor(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        ageClassCaptionLabel = new javax.swing.JLabel();
        ageClassCaptionSeparator = new javax.swing.JSeparator();
        labelLabel = new javax.swing.JLabel();
        startAgeLabel = new javax.swing.JLabel();
        endAgeLabel = new javax.swing.JLabel();
        sexLabel = new javax.swing.JLabel();
        sexComboBox = new javax.swing.JComboBox();
        labelTextField = new javax.swing.JTextField();
        startAgeTextField = new javax.swing.JTextField();
        endAgeTextField = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeClassificationEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        ageClassCaptionLabel.setFont(resourceMap.getFont("ageClassCaptionLabel.font")); // NOI18N
        ageClassCaptionLabel.setText(resourceMap.getString("ageClassCaptionLabel.text")); // NOI18N
        ageClassCaptionLabel.setName("ageClassCaptionLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
        getContentPane().add(ageClassCaptionLabel, gridBagConstraints);

        ageClassCaptionSeparator.setName("ageClassCaptionSeparator"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 5, 10);
        getContentPane().add(ageClassCaptionSeparator, gridBagConstraints);

        labelLabel.setText(resourceMap.getString("labelLabel.text")); // NOI18N
        labelLabel.setName("labelLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(labelLabel, gridBagConstraints);

        startAgeLabel.setText(resourceMap.getString("startAgeLabel.text")); // NOI18N
        startAgeLabel.setName("startAgeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(startAgeLabel, gridBagConstraints);

        endAgeLabel.setText(resourceMap.getString("endAgeLabel.text")); // NOI18N
        endAgeLabel.setName("endAgeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(endAgeLabel, gridBagConstraints);

        sexLabel.setText(resourceMap.getString("sexLabel.text")); // NOI18N
        sexLabel.setName("sexLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(sexLabel, gridBagConstraints);

        sexComboBox.setModel(new SexComboBoxModel());
        sexComboBox.setToolTipText(resourceMap.getString("sexComboBox.toolTipText")); // NOI18N
        sexComboBox.setMaximumSize(new java.awt.Dimension(29, 22));
        sexComboBox.setMinimumSize(new java.awt.Dimension(29, 22));
        sexComboBox.setName("sexComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(sexComboBox, gridBagConstraints);

        labelTextField.setText(resourceMap.getString("labelTextField.text")); // NOI18N
        labelTextField.setToolTipText(resourceMap.getString("labelTextField.toolTipText")); // NOI18N
        labelTextField.setName("labelTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(labelTextField, gridBagConstraints);

        startAgeTextField.setText(resourceMap.getString("startAgeTextField.text")); // NOI18N
        startAgeTextField.setToolTipText(resourceMap.getString("startAgeTextField.toolTipText")); // NOI18N
        startAgeTextField.setName("startAgeTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(startAgeTextField, gridBagConstraints);

        endAgeTextField.setText(resourceMap.getString("endAgeTextField.text")); // NOI18N
        endAgeTextField.setToolTipText(resourceMap.getString("endAgeTextField.toolTipText")); // NOI18N
        endAgeTextField.setName("endAgeTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(endAgeTextField, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getActionMap(ViewAgeClassificationEditor.class, this);
        okButton.setAction(actionMap.get("confirmAction")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setMaximumSize(new java.awt.Dimension(87, 23));
        okButton.setMinimumSize(new java.awt.Dimension(87, 23));
        okButton.setName("okButton"); // NOI18N
        okButton.setPreferredSize(new java.awt.Dimension(87, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(okButton, gridBagConstraints);

        cancelButton.setAction(actionMap.get("closeAction")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(cancelButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void closeAction() {
        setVisible(false);
    }

    @Action
    public void confirmAction() {
        try {
            mAgeClassification = new AgeClassification(labelTextField.getText(),
                    new Integer(startAgeTextField.getText()).intValue(),
                    new Integer(endAgeTextField.getText()).intValue(),
                    (Sex) sexComboBox.getSelectedItem());
            closeAction();
        } catch (EmptyDescriptionException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeClassificationEditor.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyLabel.text"), resourceMap.getString("errorEmptyLabel.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewAgeClassificationEditor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NegativNumberException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAgeClassificationEditor.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorNumber.text"), resourceMap.getString("errorNumber.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewAgeClassificationEditor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setAgeClassification(AgeClassification toShow) {
        labelTextField.setText( toShow.getLabel() );
        startAgeTextField.setText( String.valueOf( toShow.getAgeStart() ) );
        endAgeTextField.setText( String.valueOf( toShow.getAgeEnd() ) );
        sexComboBox.setSelectedItem( toShow.getSex() );
    }

    public AgeClassification getAgeClassification() {
        return mAgeClassification;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ageClassCaptionLabel;
    private javax.swing.JSeparator ageClassCaptionSeparator;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel endAgeLabel;
    private javax.swing.JTextField endAgeTextField;
    private javax.swing.JLabel labelLabel;
    private javax.swing.JTextField labelTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox sexComboBox;
    private javax.swing.JLabel sexLabel;
    private javax.swing.JLabel startAgeLabel;
    private javax.swing.JTextField startAgeTextField;
    // End of variables declaration//GEN-END:variables

    private AgeClassification mAgeClassification;
}
